package com.wlcorp.forcazamis.dao.stock.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ibatis.dao.client.DaoException;
import com.wlcorp.forcazamis.dao.stock.IStockProductDetailDAO;
import com.wlcorp.forcazamis.domain.stock.StockProductDetail;
import com.wlcorp.forcazamis.sys.util.DateUtil;

public class StockProductDetailDAOImpl extends HibernateDaoSupport implements IStockProductDetailDAO {

	public void deleteStockProductDetail(StockProductDetail stockProductDetail)
			throws DaoException {
		stockProductDetail.setDeleteFlag(true);
		getHibernateTemplate().update(stockProductDetail);
	}

	public StockProductDetail getStockProductDetailById(Long stockProductDetailId)
			throws DaoException {
		return (StockProductDetail)getHibernateTemplate().get(StockProductDetail.class,stockProductDetailId);
	}

	@SuppressWarnings("unchecked")
	public List getStockProductDetailList(final StockProductDetail stockProductDetail)
			throws DaoException {
		final StringBuffer queryString = new StringBuffer("select stockProductDetail from StockProductDetail as stockProductDetail where 1=1 and stockProductDetail.deleteFlag=false");
		 
		return getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(queryString.toString());
				if(null != stockProductDetail && null != stockProductDetail.getMinRownum()){
					query.setFirstResult(stockProductDetail.getMinRownum());
					query.setMaxResults(stockProductDetail.getNumPerPage());
				}
				return query.list();
			}
		});
	}

	public void saveStockProductDetail(StockProductDetail stockProductDetail) throws DaoException {
//		getHibernateTemplate().clear();
//		getHibernateTemplate().saveOrUpdate(stockProductDetail);	
//		getHibernateTemplate().refresh(stockProductDetail);
		getHibernateTemplate().merge(stockProductDetail);	
		
	}

	@SuppressWarnings("unchecked")
	public List<StockProductDetail> getListByCondition(final StockProductDetail stockProductDetail)
			throws DaoException {
		final StringBuffer queryString = new StringBuffer(
				"select stockProductDetail from StockProductDetail as stockProductDetail where 1=1 and stockProductDetail.deleteFlag=false and stockProductDetail.stockProduct.deleteFlag=false and stockProductDetail.stockProduct.stock.deleteFlag=false");
		if (null != stockProductDetail) {
			if(null!=stockProductDetail.getMyCode()){
				queryString.append(" and stockProductDetail.stockProduct.myCode like '%").append(stockProductDetail.getMyCode()).append("%'");
			}
			if(null!=stockProductDetail.getLetteringCode()){
				queryString.append(" and stockProductDetail.letteringCode like '%").append(stockProductDetail.getLetteringCode()).append("%'");
			}
			if(null!=stockProductDetail.getFactoryCode()){
				queryString.append(" and stockProductDetail.stockProduct.factoryCode like '%").append(stockProductDetail.getFactoryCode()).append("%'");
			}
			if(null!=stockProductDetail.getShelfSurefaceColor()){
				queryString.append(" and stockProductDetail.shelfSurefaceColor like '%").append(stockProductDetail.getShelfSurefaceColor()).append("%'");
			}
			if(null!=stockProductDetail.getStartDate()){
				System.out.println(stockProductDetail.getStartDate());
				queryString.append(" and stockProductDetail.stockProduct.stock.stockDate>= '").append(DateUtil.stringToSqlDate(stockProductDetail.getStartDate())).append("'");
			}
			if(null!=stockProductDetail.getEndDate()){
				System.out.println(stockProductDetail.getEndDate());
				queryString.append(" and stockProductDetail.stockProduct.stock.stockDate<= '").append(DateUtil.stringToSqlDate(stockProductDetail.getEndDate())).append("'");
			}
			
		}
		System.out.println(queryString.toString());
		return getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(queryString.toString());
				if(null != stockProductDetail && null != stockProductDetail.getMinRownum()){
					query.setFirstResult(stockProductDetail.getMinRownum());
					query.setMaxResults(stockProductDetail.getNumPerPage());
				}
				return query.list();
			}
		});
	}

	public Long getStockProductDetailCount(StockProductDetail stockProductDetail)
			throws DaoException {
		String hql="select max(stockProductDetail.productDetailId) from StockProductDetail stockProductDetail";
		return ((Long)getHibernateTemplate().iterate(hql).next()).longValue();
	}
}
